import pickle
from helper import *

class geo_checker:
	def __init__(self, helpers):
		self.geo = dict()
		self.helpers = helpers
	
	def append(self, name, flag):
		try:
			self.geo[name].add(flag)
		except KeyError:
			self.geo[name] = set([flag,])
	
	def check(self, file):
		for h in self.helpers:
			if h.check(file):
				return h
		return None
	
	def learnline(self, line, helper):
		if helper.learn(line):
			info = helper.loc.info
			for n, d, flag, t_op, t_db in info:
				self.append(n, flag)
				self.append(d, flag)
	def learn(self, file, helper = None, sep = ','):
		if not helper:
			helper = self.check(file)
		if not helper:
			return False

		fp = open(file, 'r')
		if fp:
			try:
				for line in fp:
					self.learnline(line)
			finally:
				fp.close()
	
	def load(self, file):
		pickle.load(self.geo, file)
	def save(self, file):
		pickle.dump(self.geo, file)
	def run(self, file):
		return self.check(file)

